<?php

/**
 * Twitter plugin for displaying latest tweets and posting on twitter
 *
 */
/**
 * Twitter
 *
 * @package
 * @author kubicek
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class Twitter extends Plugin
{
	// plugin info
	public $plugin_name = "Twitter";
	public $plugin_short_desc = "Twitter plugin";
	public $plugin_long_desc = "Twitter plugin displays latest tweets from specified user";
	public $version = "1.0";
	public $has_sidebar = false;
	public $has_footer = false;
	public $needs_install = true;

	private $twitter_api_url = "http://twitter.com/statuses/update.xml";
	private $username = '';
	private $password = '';
	private $tweet_text = '';

	/**
	 * Constructor
	 */
	function __construct()
	{
		$query = mysql_query("SELECT * FROM yaps_twitter");
		while($row = mysql_fetch_array($query))
		{
			if($row['name'] == 'username') $this->username = $row['value'];
			if($row['name'] == 'password') $this->password = $row['value'];
			if($row['name'] == 'tweet_text') $this->tweet_text = $row['value'];
		}
	}

	public function display_tweets()
	{
		global $theme;

		$theme->include_jquery();
		$theme->include_js($this->get_plugin_path().'twitter_plugin.js');
		$theme->js('tweet(\'http://twitter.com/statuses/user_timeline/'.$this->username.'.json?count=1&callback=?\');');
	}

	public function admin_settings()
	{
		global $core;

		$hl = $core->load_plugin('highlight');

		$datagrid = new Datagrid('yaps_twitter');
		$datagrid->draw_grid();

		echo '<p>Example of usage:</p>
'.$hl->text('$plugin = $core->load_plugin(\'twitter\');
	$plugin->display_tweets();').'<br />
<p><i>display_tweets</i> will user jQuery library and fill div with id = \'tweets\' with latest tweet from user you specified.</p>
<p>If you want to check if your theme has twitter plugin support, open <i>theme_info.php</i> and see if there is <i>$twitter = true;</i> line.</p>
<p><i>display_tweets</i> function will obviously work only in theme with <i>$twitter = true</i>. You can easily upgrade your theme for twitter plugin, just add this in place, where you want your tweet:</p>
<div class="code">
'.$hl->text(htmlspecialchars('<div id="tweets"></div>'),'html').'
</div>
<p>And in your <i>theme_info.php</i> (can be found in themes/yourthemename/) add line <i>$twitter = true;</i> or change existing line to <i>true</i>.
<p>Twitter plugin also integrates itself with Blog plugin, so you can tweet your new article with a single click! Insert your twitter username and password into table above and you can tweet your articles.</p>';
	}

	/**
	 * Twitter::install()
	 * Install function
	 * @return
	 */
	public function install()
	{
		$create_table = mysql_query("CREATE TABLE IF NOT EXISTS `yaps_twitter` (
			  `name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
			  `value` text COLLATE utf8_czech_ci NOT NULL,
			  PRIMARY KEY (`name`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci");

		$insert_data = mysql_query("INSERT INTO `yaps_twitter` (`name`, `value`) VALUES
				('username', 'your_twitter_username'),
				('password', 'your_twitter_password'),
				('tweet_text', 'Yaps! tweet: I have new article ')");

		$insert_data_2 = mysql_query("INSERT INTO yaps_config VALUES('plugin_twitter_installed','true')");

		if($create_table && $insert_data && $insert_data_2) return true;
		else return false;
	}

	public function tweet($name,$url,$tweet_text = 'default')
	{
		global $core;

		$url = $this->shorten_url(WEB_URL.$url);

		if($tweet_text == 'default') $base = $this->tweet_text;
		else $base = $tweet_text;

		$twitter_data = $base.$name.' - '.$url;

		if(strlen($twitter_data) > 140)
		{
			$core->err('Sorry, but your Tweet is longer than 140 chars.');
		}
		else
		{
			$out = "POST http://twitter.com/statuses/update.json HTTP/1.1\r\n"
		  ."Host: twitter.com\r\n"
		  ."Authorization: Basic ".base64_encode ($this->username.':'.$this->password)."\r\n"
		  ."Content-type: application/x-www-form-urlencoded\r\n"
		  ."Content-length: ".strlen ("status=$twitter_data")."\r\n"
		  ."Connection: Close\r\n\r\n"
		  ."status=".$twitter_data;

			$fp = fsockopen ('twitter.com', 80);
			fwrite ($fp, $out);
			fclose ($fp);

			$core->msg('sending: '.$out);
		}
	}

	/**
	 * Twitter::shorten_url()
	 * Using u.nu to shorten url for twitter
	 * Requires cURL lib to be installed on Apache
	 * @param mixed $url
	 * @return
	 */
	public function shorten_url($url)
	{
		$url = 'http://is.gd/api.php?longurl='.urlencode($url);
		$url = file_get_contents($url);
		return trim($url);
	}
}

?>